package com.it.cloud.generator.result;

import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.it.cloud.common.R;
import com.it.cloud.common.vo.TreeNode;
import com.it.cloud.dao.BaseDao;
import ${entityImport};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

@RestController
@AllArgsConstructor
@Api(tags={"${name}"})
public class ${name}Controller {
	
	private BaseDao baseDao;
	private ${name}Service ${name?uncap_first}Service;
	
	@PostMapping("${mapping}/add")
	@ApiOperation(value = "增加")
	@ApiOperationSupport(order = 1)
	public R<TreeNode> add(Long pid) {
		return ${name?uncap_first}Service.addChild(pid);
	}
	
	@PostMapping("${mapping}/del")
	@ApiOperation(value = "删除")
	@ApiOperationSupport(order = 2)
	public R<?> del(Long id) {
		baseDao.delById(${name}.class, id);
		return R.success();
	}
	
	@PostMapping("${mapping}/update")
	@ApiOperation(value = "修改")
	@ApiOperationSupport(order = 3)
	public R<${name}> update(${name} obj) {
		${name} oldObj = baseDao.findById(${name}.class, obj.getId());
		if(${name?uncap_first}Service.isNameExist(obj.getId(), obj.getName())) {
			return R.error("名称在这一层已经存在！");
		}
		if(${name?uncap_first}Service.isCodeExist(obj.getId(), obj.getCode())) {
			return R.error("编码在全局已经存在！");
		}
		oldObj.setName(obj.getName());
		oldObj.setCode(obj.getCode());
		baseDao.update(oldObj);
		return R.success(oldObj);
	}
	
	@PostMapping("${mapping}/drop")
	@ApiOperation(value = "移动")
	@ApiOperationSupport(order = 4)
	public R<?> drop(Long dragId, Long dropId, Integer position) {
		${name} dragObj = baseDao.findById(${name}.class, dragId);
		${name} dropObj = baseDao.findById(${name}.class, dropId);
		if(position==0) {
			${name?uncap_first}Service.moveInner(dragObj, dropObj);
		}else if(position>0) {
			${name?uncap_first}Service.moveAfter(dragObj, dropObj);
		}else {
			${name?uncap_first}Service.moveBefore(dragObj, dropObj);
		}
		return R.success();
	}
	
	@GetMapping("${mapping}/first")
	@ApiOperation(value = "查询第一层")
	@ApiOperationSupport(order = 5)
	public R<List<TreeNode>> first() {
		List<${name}> list = ${name?uncap_first}Service.findChild(0L);
		List<TreeNode> nodeList = ${name?uncap_first}Service.genTreeNodeList(list);
		return R.success(nodeList);
	}
	
	@GetMapping("${mapping}/child")
	@ApiOperation(value = "查询下一层")
	@ApiOperationSupport(order = 6)
	public R<List<TreeNode>> child(Long pid) {
		List<${name}> list = ${name?uncap_first}Service.findChild(pid);
		List<TreeNode> nodeList = ${name?uncap_first}Service.genTreeNodeList(list);
		return R.success(nodeList);
	}
	
	@GetMapping("${mapping}/info")
	@ApiOperation(value = "详情")
	@ApiOperationSupport(order = 7)
	public R<${name}> info(Long id) {
		${name} obj = baseDao.findById(${name}.class, id);
		return R.success(obj);
	}
}